package com.zlr.hiring.talent.servlet;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.zlr.hiring.common.util.MD5Util;
import com.zlr.hiring.talent.dao.TalentDao;
import com.zlr.hiring.talent.entity.TalentEntity;

public class TalentServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	private TalentDao talentDao = new TalentDao();

	public TalentServlet() {
		super();

	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		this.doPost(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		String command = request.getParameter("command");
		if ("register".equals(command)) {
			register(request, response);
		} else if ("login".equals(command)) {
			login(request, response);
		} else if ("get".equals(command)) {
			get(request, response);
		} else if ("main".equals(command)) {
			main(request, response);
		} else if ("logout".equals(command)) {
			logout(request, response);
		} else {
			throw new IllegalStateException("请求非法====");
		}
	}

	private void register(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		// 获取前台传来的参数
		String code = request.getParameter("code");
		String password = request.getParameter("password");
		String name = request.getParameter("name");
		String phone = request.getParameter("phone");
		String email = request.getParameter("email");
		int age = Integer.parseInt(request.getParameter("age"));
		int gender = Integer.parseInt(request.getParameter("gender"));
		StringBuilder sb = new StringBuilder();
		String[] hobbies = request.getParameterValues("hobby");
		for (int i = 0; i < hobbies.length; i++) {
			if (i == hobbies.length - 1) {
				sb.append(i);
			} else {
				sb.append(hobbies[i] + "|");
			}
		}

		/**
		 * 调用业务逻辑
		 */
		TalentEntity talent = new TalentEntity();
		talent.setCode(code);
		talent.setPassword(password);
		talent.setName(name);
		talent.setPhone(phone);
		talent.setEmail(email);
		talent.setAge(age);
		talent.setGender(gender);
		talent.setHobby(sb.toString());
		talentDao.register(talent);

		request.getRequestDispatcher("view/talent/talent-login.jsp").forward(
				request, response);
	}

	/**
	 * get
	 * 
	 * @throws IOException
	 * @throws ServletException
	 */

	private void get(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		int id = Integer.parseInt(request.getParameter("id"));

		TalentEntity talentEntity = talentDao.get(id);

		request.setAttribute("talentEntity", talentEntity);
		request.getRequestDispatcher("/view/talent/talent-info.jsp").forward(
				request, response);
	}

	private void login(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		String code = request.getParameter("code");
		String password = request.getParameter("password");
		String validateCode = request.getParameter("validateCode");
		String kaptchaSessionKey = (String) request.getSession().getAttribute(
				com.google.code.kaptcha.Constants.KAPTCHA_SESSION_KEY);

		if (!kaptchaSessionKey.equals(validateCode)) {
			request.setAttribute("code", code);
			request.setAttribute("errorInfo", "验证码输入错误!");
			request.getRequestDispatcher("view/talent/talent-login.jsp")
					.forward(request, response);

		}

		TalentEntity talent = talentDao.login(code, MD5Util.toMD5(password));

		if (talent == null) {
			request.setAttribute("code", code);
			request.setAttribute("errorInfo", "用户名或密码错误！");
			request.getRequestDispatcher("view/talent/talent-login.jsp")
					.forward(request, response);
		} else {
			request.getSession().setAttribute("talent", talent);
			response.sendRedirect("TalentServlet?command=main");
		}

	}

	private void main(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		String rowStr = request.getParameter("row");

		if (rowStr != null) {
			int row = Integer.parseInt(rowStr);
			if (row == 1) {
				request.setAttribute("success", "操作成功！");
			} else {
				request.setAttribute("error", "操作失败！");
			}
		}

		request.getRequestDispatcher(
				"ApplicationServlet?command=listTalentApplyByPage").forward(
				request, response);
	}

	private void logout(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		request.getSession().invalidate();
		request.getRequestDispatcher("view/talent/talent-login.jsp").forward(
				request, response);
	}
}
